import 'package:blog/http/Request1.dart';
import 'package:blog/http/http_request.dart';
import 'package:blog/http/request.dart';
import 'package:blog/http/request_api.dart';
import 'package:blog/http/request_api1.dart';
import 'package:blog/model/banner_mode.dart';
import 'package:blog/model/collect_model.dart';
import 'package:blog/model/hotword_mode.dart';
import 'package:blog/model/lightcloud/condition_card.dart';
import 'package:blog/model/lightcloud/searchCard.dart';
import 'package:blog/model/lightcloud/website_info.dart';
import 'package:blog/model/music_card_model.dart';
import 'package:blog/model/points_detail.dart';
import 'package:blog/model/project_model.dart';
import 'package:blog/model/lightcloud/recommend_card.dart';
import 'package:blog/model/request_ranking.dart';
import 'package:blog/model/request_register.dart';
import 'package:blog/model/wechat_public_mode.dart';
import 'package:blog/util/function.dart';
import 'package:blog/util/save/sp_util.dart';

typedef SuccessOver<T> = Function(T data, bool over);

/// @class : RequestRepository
/// @date : 2021/08/25
/// @name : CTGU_LLZ(404name)
/// @description : 请求玩安卓api--> 仓库
class RequestRepository {
  //-------------------------轻云社区API--------------------------------
  /// 请求首页文章
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestRecommendCard(
      int page, {
        SuccessOver<List<RecommendCard>>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.apiCard + "?current=" + page.toString(), {},
        dialog: false, success: (data) {
      RecommendCardResult list  = RecommendCardResult.fromJson(data);
      print(list);
      if (success != null) {
        success(list.data, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {

        fail(code, msg);
      }
    });
  }
  /// 请求互助页
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestHelpCard(
      int page, {
        SuccessOver<List<ArticlePreviewDTOList>>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.studyCard + "&current=" + page.toString(), {},
        dialog: false, success: (data) {
      ConditonCardResult list  = ConditonCardResult.fromJson(data);
      print(list);
      if (success != null) {
        success(list.data.articlePreviewDTOList, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  /// 请求商品
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestGoodsCard(
      int page, {
        SuccessOver<List<ArticlePreviewDTOList>>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.shopCard + "&current=" + page.toString(), {},
        dialog: false, success: (data) {
      ConditonCardResult list  = ConditonCardResult.fromJson(data);
      print(list);
      if (success != null) {
        success(list.data.articlePreviewDTOList, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  /// 请求热门
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestHotCard(
      int page, {
        SuccessOver<List<ArticlePreviewDTOList>>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.hotCard + "&current=" + page.toString(), {},
        dialog: false, success: (data) {
      ConditonCardResult list  = ConditonCardResult.fromJson(data);
      print(list);
      if (success != null) {
        success(list.data.articlePreviewDTOList, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  /// 请求首页文章
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  websiteInfo(
      {
        SuccessOver<WebsiteInfo>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.websiteInfo, {},
        dialog: false, success: (data) {
      WebsiteInfoResult webInfo  = WebsiteInfoResult.fromJson(data);
      print(webInfo);
      if (success != null) {
        success(webInfo.data, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  /// 搜索文章
  /// [id] page
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestSearch(
      int? page,
      String? hotWord,
      {
        SuccessOver<List<SearchCard>>? success,
        Fail? fail,
      }) {
    Request1.get<dynamic>(
        RequestApi1.search + 'keywords=' + hotWord! + '&current=' + page.toString(), {},
        dialog: false, success: (data) {
      SearchCardResult searchCardResult  = SearchCardResult.fromJson(data);
      print(searchCardResult);
      if (searchCardResult != null) {
        success!(searchCardResult.data, false);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  //-------------------------玩安卓api--------------------------------
  ///登录请求
  /// [account]账号
  /// [password]密码
  /// [password]重复密码
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  register(
    String account,
    String password,
    String rePassword, {
    Success<UserEntity>? success,
    Fail? fail,
  }) {
    Request.post<dynamic>(RequestApi.apiRegister, {
      "username": account,
      "password": password,
      "repassword": rePassword
    }, success: (data) {
      var registerInfo = UserEntity.fromJson(data);
      registerInfo.password = password;
      SpUtil.putUserInfo(registerInfo);
      if (success != null) {
        success(registerInfo);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///登录请求
  /// [account]账号
  /// [password]密码
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  login(
    String account,
    String password, {
    Success<UserEntity>? success,
    Fail? fail,
  }) {
    Request.post<dynamic>(
        RequestApi.apiLogin, {"username": account, "password": password},
        success: (data) {
          var loginInfo = UserEntity.fromJson(data);
          loginInfo.password = password;
          SpUtil.putUserInfo(loginInfo);
          if (success != null) {
            success(loginInfo);
          }
        }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///收藏|取消收藏 文章接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  getUserInfo({
    Success<UserEntity>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(RequestApi.apiUserInfo, {}, dialog: false,
        success: (data) {
      if (success != null) {
        var userInfo = data["userInfo"];
        userInfo = UserEntity.fromJson(userInfo);
        success(userInfo);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///收藏|取消收藏 文章接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  collectArticle(
    int id, {
    bool isCollect = false,
    Success<bool>? success,
    Fail? fail,
  }) {
    var api = isCollect ? RequestApi.apiUnCollect : RequestApi.apiCollect;
    Request.post<dynamic>(api.replaceFirst(RegExp('id'), '$id'), {},
        dialog: false, success: (data) {
      if (success != null) {
        success(true);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求项目列表接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestTabModule(
    int page, {
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiProject.replaceFirst(RegExp('page'), '$page'), {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求积分排行榜接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  rankingPoints(
    int page, {
    SuccessOver<List<Ranking>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiRanking.replaceFirst(RegExp('page'), '$page'), {},
        dialog: false, success: (data) {
      ///先解析外层分页
      ProjectPage pageData = ProjectPage.fromJson(data);

      ///数组解析
      var list = pageData.datas.map((value) {
        return Ranking.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求积分排行榜接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  pointsDetail(
    int page, {
    SuccessOver<List<Points>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiPoints.replaceFirst(RegExp('page'), '$page'), {},
        dialog: false, success: (data) {
      ///先解析外层分页
      ProjectPage pageData = ProjectPage.fromJson(data);

      ///数组解析
      var list = pageData.datas.map((value) {
        return Points.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///我收藏的文章列表
  ///此处page 必须减去1，此处从0开始
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  collectDetail(
    int page, {
    SuccessOver<List<CollectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiCollectDetail.replaceFirst(RegExp('page'), '${page - 1}'),
        {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return CollectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///退出登录
  exitLogin({
    Success<bool>? success,
    Fail? fail,
  }) {
    Request.post<dynamic>(RequestApi.apiLogout, {}, dialog: false,
        success: (data) {
      if (success != null) {
        success(true);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求问答接口
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestAskModule(
    int page, {
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiAsk.replaceFirst(RegExp('page'), '$page'), {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
  ///请求音乐卡片接口
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestMusicCardModule(
      int page, {
        SuccessOver<List<MusicCardData>>? success,
        Fail? fail,
      }) {
    Request.get<dynamic>(
        RequestApi.musicCard.replaceFirst(RegExp('page'), '$page'), {},
        dialog: false, success: (data) {

      List<MusicCardData> temp = [];
        success!(temp, false);

    }, fail: (code, msg) {
      List<MusicCardData> temp = [];
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/Florence.mp3",
          imgUrl: "http://p1.music.126.net/RSSgYT8T9XPMd_aPPZZXJQ==/109951163346110906.jpg",
          musicName: "Florence",
          musicDes: "第一首歌"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/Kako - 春泥棒.mp3",
          imgUrl: "http://p1.music.126.net/kBtj7HlFDuokeO4iyHTXaA==/109951165616927101.jpg",
          musicName: "Kako - 春泥棒",
          musicDes: "第一首歌"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/夏令时记录.mp3",
          imgUrl: "http://p1.music.126.net/mqYM_A9Y990c-eRVnaGUhg==/109951165050105840.jpg",
          musicName: "夏令时记录",
          musicDes: "夏令时记录"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/One_Last_Kiss.mp3",
          imgUrl: "http://p2.music.126.net/l3G4LigZnOxFE9lB4bz_LQ==/109951165791860501.jpg",
          musicName: "One_Last_Kiss",
          musicDes: "One_Last_Kiss"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/白日-King_Gnu.mp3",
          imgUrl: "http://p1.music.126.net/ZPnyNCu7uUUNW3MsuMpJqA==/109951165048502700.jpg",
          musicName: "白日-King_Gnu",
          musicDes: "白日-King_Gnu"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/Lemon-米津玄师.mp3",
          imgUrl: "http://p2.music.126.net/jtPZRUFrSS-nRCjW_LYowQ==/109951166521931227.jpg",
          musicName: "Lemon-米津玄师",
          musicDes: "Lemon-米津玄师"));
      temp.add(new MusicCardData(
          musicUrl: "https://minio.404name.top/music/BOY-King_Gnu.mp3",
          imgUrl: "http://p2.music.126.net/qgVg7PrKXxivrUn3xZ1eUA==/109951166474269680.jpg",
          musicName: "BOY-King_Gnu",
          musicDes: "BOY-King_Gnu"));
      if (success != null) {
        success(temp, false);
      }
    });

  }
  ///请求广场列表接口
  /// [page] 当前页面
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestSquareModule(
    int page, {
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiSquare.replaceFirst(RegExp('page'), '${page - 1}'), {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///获取首页的Banner图片
  getBanner({
    Success<List<Banners>>? success,
    Fail? fail,
  }) {
    Request.get<List<dynamic>>(RequestApi.yuqueApiBanner, {}, dialog: false,
        success: (data) {

      //print(data);
          print(data[0]["placements"][0]["blocks"][0]["data"]);
      if (success != null) {
        var list = data[0]["placements"][0]["blocks"][0]["data"].map((value) {
          return Banners.fromJson(value);
        }).toList().cast<Banners>();
        print(list);
        success(list);
      }
    }, fail: (code, msg) {
          print("语雀请求失败！");
          print(msg);
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///获取搜索热词
  getSearchHotWord({
    Success<List<HotWord>>? success,
    Fail? fail,
  }) {
    Request.get<List<dynamic>>(RequestApi.apiHotWord, {}, dialog: false,
        success: (data) {
      if (success != null) {
        var list = data.map((value) {
          return HotWord.fromJson(value);
        }).toList();
        success(list);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///根据关键词搜索文章
  /// [page]当前页码
  /// [hotWord] 当前热词
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  searchKeyWord(
    int page,
    String hotWord, {
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.post<Map<String, dynamic>>(
        RequestApi.apiSearchWord.replaceFirst(RegExp('page'), '$page'),
        {
          "k": hotWord,
        },
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求首页文章列表接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestHomeArticle(
    int page, {
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiHome.replaceFirst(RegExp('page'), '${page - 1}'), {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///获取微信公众号列表
  getWechatPublic({
    Success<List<WechatPublic>>? success,
    Fail? fail,
  }) {
    Request.get<List<dynamic>>(RequestApi.apiWechatPublic, {}, dialog: false,
        success: (data) {
      if (success != null) {
        var list = data.map((value) {
          return WechatPublic.fromJson(value);
        }).toList();
        success(list);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  /// 分享文章到站点
  /// [title] 文章标题
  /// [link] 文章链接
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  shareArticle(
    String title,
    String link, {
    Success<String>? success,
    Fail? fail,
  }) {
    Request.post(RequestApi.apiAddArticle, {'title': title, 'link': link},
        dialog: false, success: (data) {
      if (success != null) {
        success("success");
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }

  ///请求首页文章列表接口
  ///[id]文章ID
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  requestShareArticleList(
    int page, {
    ParamSingleCallback<int>? length,
    SuccessOver<List<ProjectDetail>>? success,
    Fail? fail,
  }) {
    Request.get<dynamic>(
        RequestApi.apiShareArticleList.replaceFirst(RegExp('page'), '$page'),
        {},
        dialog: false, success: (data) {
      ProjectPage pageData = ProjectPage.fromJson(data['shareArticles']);
      var list = pageData.datas.map((value) {
        return ProjectDetail.fromJson(value);
      }).toList();
      if (success != null) {
        success(list, pageData.over);
      }
      if(length != null){
        length(pageData.total);
      }
    }, fail: (code, msg) {
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
}
